<?php

class Module_Task_Casecenter
{

    //撤案先提交到这里,在循环再扔 到redis 中
    public static function withdrawtaskHandler($data)
    {
        if (empty($data['case_id_str'])) {
            return ['code' => 4000, 'message' => '缺少参数', 'data' => []];
        }
        try {

            $ssid = empty($data['ssid']) ? str_replace('.', '', uniqid('', true)) : $data['ssid'];
            $case_id_str = $data['case_id_str'];

            $case_id_arr = explode(",", $case_id_str);
            foreach ($case_id_arr as $k => $case_id) {
                $data['case_id'] = $case_id;
                $post_data = array(
                    'type' => 'Module_Task_Casecenter',
                    'method' => 'withdraw',
                    'data' => $data
                );
                @Module_Queue_Base::addQueue($post_data);
            }

            Func_Log::write('withdraw', "withdrawTask,ssid: " . $ssid . 'case save success', 'res= ' . json_encode($data));
            return ['code' => 200, 'message' => '案件入库成功', 'data' => []];
        } catch (Error $err) {//捕获异常
            Func_Log::write('withdraw', "withdrawTask,ssid: " . $ssid . 'case save server error :' . $err, 'res= ' . json_encode($data));
            return ['code' => 501, 'message' => '案件入库失败', 'data' => []];
        } catch (Exception $e) {//捕获异常
            Func_Log::write('withdraw', "withdrawTask,ssid: " . $ssid . 'case save server error :' . $e, 'res= ' . json_encode($data));
            return ['code' => 501, 'message' => '案件入库失败', 'data' => []];
        }
    }

    //第二次 单个处理 撤案了
    public static function withdrawHandler($data)
    {
        if (empty($data['case_id'])) {
            return ['code' => 4000, 'message' => '缺少参数', 'data' => []];
        }
        try {
            $ssid = empty($data['ssid']) ? str_replace('.', '', uniqid('', true)) : $data['ssid'];
            $case_id = $data['case_id'];

            $updataCase = [
                'sysStatus' => 2,
                'id' => $case_id,
            ];
            $log = [
                'caseId' => $case_id,
                'status' => 'update_expire_time',
                'operationUid' => $data['operationUid'],
                'operationName' => $data['operationName'],
                'operationContent' => '案件撤案',
                'remark' => '将案件修改为撤案状态',
                'createTime' => time(),
            ];
            $res = Module_Mediate_Case::getInstance()->updateStatusAddLogById($updataCase, $log);
            if (!$res) {
                Func_Log::write('withdraw', "ssid: " . $ssid . 'case save error', '$updataCase= ' . json_encode($updataCase) . ',$log=' . json_encode($log));
            }


            Func_Log::write('withdraw', "ssid: " . $ssid . 'case save success', 'res= ' . json_encode($data));
            return ['code' => 200, 'message' => '案件入库成功', 'data' => []];
        } catch (Error $err) {//捕获异常
            Func_Log::write('withdraw', "ssid: " . $ssid . 'case save server error :' . $err, 'res= ' . json_encode($data));
            return ['code' => 501, 'message' => '案件入库失败', 'data' => []];
        } catch (Exception $e) {//捕获异常
            Func_Log::write('withdraw', "ssid: " . $ssid . 'case save server error :' . $e, 'res= ' . json_encode($data));
            return ['code' => 501, 'message' => '案件入库失败', 'data' => []];
        }
    }



    //转案
    public static function transferCaseHandler($data)
    {
        if (empty($data['case_id_str'])) {
            return ['code' => 4000, 'message' => '缺少参数', 'data' => []];
        }
        try {
            $ssid = empty($data['ssid']) ? str_replace('.', '', uniqid('', true)) : $data['ssid'];
            $id_str = $data['case_id_str'];
            $mediators_id = $data['mediators_id'];
            $mediationOrganId = $data['mediationOrganId'];

            if (!empty($mediators_id)) {
                $mediator = Module_User_Administrator::getInstance()->getItemById($mediators_id);
                if (!$mediator) {
                    AjaxTool::returnJsonData(4001);
                }
            }
            if (!empty($mediationOrganId)) {
                $mediationOrgan = Module_InfoBase_MediationOrgan::getInstance()->getItemById($mediationOrganId, true);
            }

            $id_arr = explode(",", $id_str);
            foreach ($id_arr as $case_id) {
                //只分配给到组
                if(!empty($mediationOrganId) && empty($mediators_id)){
                    $updataCase = [
                        'mediationOrganId' => $mediationOrganId,
                        'mediatorId' => '',
                        'mediatorName' => '',
                        'id' => $case_id,
                    ];
                }elseif(!empty($mediationOrganId) && !empty($mediators_id)){
                    $updataCase = [
                        'mediationOrganId' => $mediationOrganId,
                        'mediatorId' => $mediator['id'],
                        'mediatorName' => $mediator['name'],
                        'id' => $case_id,
                    ];
                }elseif(empty($mediationOrganId) && !empty($mediators_id)){
                    $updataCase = [
                        'mediatorId' => $mediator['id'],
                        'mediatorName' => $mediator['name'],
                        'id' => $case_id,
                    ];
                }else{
                    Func_Log::write('transferCaseAction', 'case save error', '$mediationOrganId= ' . json_encode($mediationOrgan) . ',$mediators_id=' . json_encode($mediator));
                    continue;
                }

                if (!empty($mediator)) {
                    $log = [
                        'caseId' => $case_id,
                        'status' => 'case_transfer',
                        'operationUid' => $data['operationUid'],
                        'operationName' => $data['operationName'],
                        'operationContent' => '转移案件',
                        'remark' => "将案件转交给调解员{$mediator['name']},调解员id为：{$mediator['id']}",
                        'createTime' => time(),
                    ];
                } else {
                    $log = [
                        'caseId' => $case_id,
                        'status' => 'case_transfer',
                        'operationUid' => $data['operationUid'],
                        'operationName' => $data['operationName'],
                        'operationContent' => '转移案件组',
                        'remark' => "将案件转交给调解组{$mediationOrgan['name']}",
                        'createTime' => time(),
                    ];
                }

                $res = Module_Mediate_Case::getInstance()->updateStatusAddLogById($updataCase, $log);
                if (!$res) {
                    Func_Log::write('transferCaseAction', 'case save error', '$updataCase= ' . json_encode($updataCase) . ',$log=' . json_encode($log));
                }

            }

            Func_Log::write('withdraw', "withdrawTask,ssid: " . $ssid . 'case save success', 'res= ' . json_encode($data));
            return ['code' => 200, 'message' => '案件入库成功', 'data' => []];
        } catch (Error $err) {//捕获异常
            Func_Log::write('withdraw', "withdrawTask,ssid: " . $ssid . 'case save server error :' . $err, 'res= ' . json_encode($data));
            return ['code' => 501, 'message' => '案件入库失败', 'data' => []];
        } catch (Exception $e) {//捕获异常
            Func_Log::write('withdraw', "withdrawTask,ssid: " . $ssid . 'case save server error :' . $e, 'res= ' . json_encode($data));
            return ['code' => 501, 'message' => '案件入库失败', 'data' => []];
        }
    }

    //更新委案到期时间
    public static function entrustExpireTimeHandler($data)
    {
        if (empty($data['case_id_str'])) {
            return ['code' => 4000, 'message' => '缺少参数', 'data' => []];
        }
        try {
            $ssid = empty($data['ssid']) ? str_replace('.', '', uniqid('', true)) : $data['ssid'];
            $id_str = $data['case_id_str'];


            $id_arr = explode(",", $id_str);
            foreach ($id_arr as $case_id) {
                $case = Module_Mediate_Case::getInstance()->getItemById($case_id);
                $updataCase=[
                    'entrustExpireTime'=>$data['entrustExpireTime'],
                    'id'=>$case_id,
                ];
                $log= [
                    'caseId' => $case['id'],
                    'status' => 'update_expire_time',
                    'operationUid' => $data['operationUid'],
                    'operationName' => $data['operationName'],
                    'operationContent' => '更新委案到期时间',
                    'remark' => '从' . date("Y-m-d H:i:s", $case['entrustExpireTime']) . '更新为' . $data['entrustExpireTime'],
                    'createTime' => time(),
                ];


                $res = Module_Mediate_Case::getInstance()->updateStatusAddLogById($updataCase, $log);
                if (!$res) {
                    Func_Log::write('entrustExpireTime', 'case save error', '$updataCase= ' . json_encode($updataCase) . ',$log=' . json_encode($log));
                }

            }

            Func_Log::write('entrustExpireTime', "entrustExpireTime,ssid: " . $ssid . 'case save success', 'res= ' . json_encode($data));
            return ['code' => 200, 'message' => '案件入库成功', 'data' => []];
        } catch (Error $err) {//捕获异常
            Func_Log::write('entrustExpireTime', "withdrawTask,ssid: " . $ssid . 'case save server error :' . $err, 'res= ' . json_encode($case));
            return ['code' => 501, 'message' => '案件入库失败', 'data' => []];
        } catch (Exception $e) {//捕获异常
            Func_Log::write('entrustExpireTime', "withdrawTask,ssid: " . $ssid . 'case save server error :' . $e, 'res= ' . json_encode($case));
            return ['code' => 501, 'message' => '案件入库失败', 'data' => []];
        }
    }

}

